package com.demo.autotest.controller;

import com.demo.autotest.common.utils.PageUtils;
import com.demo.autotest.database.domain.CommonParam;
import com.demo.autotest.database.domain.Function;
import com.demo.autotest.dto.CommonParamDTO;
import com.demo.autotest.dto.FunctionDTO;
import com.demo.autotest.dto.PageDTO;
import com.demo.autotest.request.QueryRequest;
import com.demo.autotest.service.CommonParamService;
import com.demo.autotest.service.FunctionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController // 定义一个controller类
@RequestMapping("/autotest/function")
public class FunctionController {

    @Resource
    private FunctionService functionService;

    @PostMapping(value = "/save")
    public void saveParam(@RequestBody Function function, HttpServletRequest request){
        String user = request.getSession().getAttribute("userId").toString();
        function.setUpdateUser(user);
        functionService.saveFunction(function);
    }

    @PostMapping("/delete/{functionId}")
    public void delete(@PathVariable String functionId){
        functionService.delete(functionId);
    }

    @PostMapping("/detail/{functionId}")
    public Function getFunctionDetail(@PathVariable String functionId){
        return functionService.getFunctionDetail(functionId);
    }

    @PostMapping("/list/{pageNum}/{pageSize}")
    public PageDTO<List<FunctionDTO>> getFunctionList(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                                         @RequestBody QueryRequest queryRequest){
        Page<Object> page = PageHelper.startPage(pageNum, pageSize, true);
        return PageUtils.setPageInfo(page, functionService.getFunctionList(queryRequest));
    }
}
